import { createRouter,createWebHashHistory } from 'vue-router'
import Home from '../views/home.vue'
import Login from '../views/login.vue'
import { useUserStore } from '@/stores/user'

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home
    },
    {
      path: '/login',
      name: 'login',
      component:Login
    }
  ]
})
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  if (to.path === '/login') return next()
  const token = userStore.userInfo.username
  if (token!=='') return next()
  return next('/login')
})

export default router
